<template>
  <!-- 别名弹出框 -->
  <view class="alias" @click.stop="">
    <view class="header">
      <view class="line">
        <view></view>
      </view>
      <view class="title"
        >项目别名
        <text>(注: 项目别名一般为使用单位对该项目命名)</text>
      </view>
    </view>
    <!-- 中间部分 -->
    <view class="content">
      <view class="search">
        <view class="searchInput">
          <input
            type="text"
            placeholder="请输入项目别名"
            @input="removeEmptyName"
            v-model="name"
          />
        </view>
        <view class="addBtn" @click="handleAddName">添加</view>
      </view>
      <view class="list" v-if="list.length">
        <view :class="['listItem',index==list.length-1?'noBorder':'']" v-for="(item, index) in list" :key="index">
          <view class="name">{{ item.alias }}</view>
          <view class="del" @click.stop="handleDel(index)">
            <image src="@/static/images/delete.png" mode="" />
          </view>
        </view>
      </view>
      <view class="emptyList" v-else>
        <image src="@/static/images/index/empty1.png" mode="" />
        ~ 暂无别名 ~
      </view>
    </view>
    <view class="bottom">
      <view :class="[list.length ? 'active' : '']" @click="handleComfire"
        >确定</view
      >
    </view>
  </view>
</template>

<script>
export default {
	data() {
		return {
			list: [],
			name: '',
		};
	},
	methods: {
		removeEmptyName(e) {
			console.log(e.detail.value);
			this.name = e.detail.value.replace(/\s+/g, '');
		},
		// 添加名字
		handleAddName() {
			// console.log(this.name.length)
			if (this.name.length) {
				this.list.push({ alias: this.name });
				console.log(this.list);
				this.name = '';
			} else {
				uni.showToast({
					title: '请输入项目别名',
					icon: 'none',
				});
			}
		},
		// 删除别名
		handleDel(i) {
			this.list.splice(i, 1);
		},
		init(list) {
			//    赋值list
			this.list = JSON.parse(JSON.stringify(list));
		},
		handleComfire() {
			if (!this.list.length) {
				return;
			}
			let aliasList = this.list.map((item) => {
				return {
					alias: item.alias,
				};
			});
			// 下面是确认按钮
			this.$emit('comfireAlias', aliasList);
		},
	},
};
</script>

<style lang="scss" scoped>
.alias {
  background: #fff;
  border-radius: 16rpx 16rpx 0 0;
  .header {
    padding-top: 20rpx;
    .line {
      width: 100%;
      display: flex;
      justify-content: center;
      view {
        width: 64rpx;
        height: 10rpx;
        background: #d8d8d8;
        border-radius: 6rpx;
      }
    }
    border-bottom: 2rpx solid #eeeeee;
    .title {
      color: #333333;
      font-weight: 600;
      font-size: 32rpx;
      padding: 26rpx 40rpx 20rpx 40rpx;
      text {
        color: #999;
        font-size: 24rpx;
        font-weight: normal;
      }
    }
  }
  .content {
    padding: 16rpx 40rpx;
    height: 586rpx;
    overflow: auto;
    // background: pink;
    .search {
      width: 670rpx;
      height: 64rpx;
      background: #f6f6f6;
      border-radius: 8rpx;
      display: flex;
      .searchInput {
        padding: 0rpx 16rpx;
        display: flex;
        flex: 1;
        align-items: center;
        input {
          font-size: 26rpx;
          width: 100%;
          height: 36rpx;
        }
      }

      .addBtn {
        display: flex;
        width: 104rpx;
        background: #0091ff;
        border-radius: 0rpx 8rpx 8rpx 0rpx;
        color: #fff;
        justify-content: center;
        align-items: center;
        font-size: 28rpx;
      }
    }
    .list {
      .listItem {
        display: flex;
        padding: 24rpx 0;
        justify-content: space-between;
        border-bottom: 2rpx solid #eeeeee;
        .name {
          width: 618rpx;
        }
        .del {
          display: flex;
          flex: 1;
          justify-content: flex-end;
          image {
            width: 32rpx;
            height: 32rpx;
          }
        }
      }
      .noBorder{
        border-bottom:none!important
      }
    }
    .emptyList {
      display: flex;
      flex-direction: column;
      align-items: center;
      color: #999;
      font-size: 26rpx;
      margin-top: 120rpx;
      image {
        width: 64rpx;
        height: 64rpx;
        margin-bottom: 8rpx;
      }
    }
  }
  .bottom {
    padding: 14rpx 40rpx 26rpx 40rpx;
    border-top: 2rpx solid #eeeeee;
    view {
      width: 670rpx;
      height: 70rpx;
      background: #efefef;
      border-radius: 8rpx;
      color: #ccc;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .active {
      background: #0091ff;
      color: #fff;
    }
  }
}
</style>
